<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
model('jy_scrm/model')->check_custom();
//注册电商客会员
$return['user_buystatus']=$this->user_buystatus;
if($_RQ['op']=='main'){
	$id=intval($_RQ['id']);
	$userid=$_RQ['userid'];
	if($userid){
		$item=pdo_get('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'external_userid'=>$userid,'uniacid'=>$_SESSION['uniacid']));
		if(!$item['id']){
			$id=model('jy_scrm/member')->up_member_item($userid);
		}
	}
	if(!$item['id'] && $id){
		$item=pdo_get('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(!$item['mid'] && $item['unionid']){
		$mid=model('jy_scrm/member')->get_unionidmid($item['unionid']);
		pdo_update('jy_scrm_member',array('mid'=>$mid),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$item['mid']=$mid;
	}
	$this->session('user_mid',$item['mid']);
	$item['mymembernum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'external_userid'=>$_SESSION['openid']));
	$item['myqunnum']=pdo_count('jy_scrm_qun',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'owner'=>$_SESSION['openid']));	
	$item['daymembernum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'external_userid'=>$_SESSION['openid'],'createtime >'=>$daytime));
	$item['daygenjinnum']=pdo_count('jy_scrm_worker_linelog',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'userid'=>$item['id'],'createtime >'=>$daytime));
	if($item['daygenjinnum']){
		$parameter=array(
				'params'=>array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'userid'=>$item['id']),
				'orderby'=>'createtime asc',
				'limit'=>'1',
		);
		$linelog=$this->get_list('worker_linelog',$parameter);
		$item['linestart']=$linelog[0]['createtime'];
		$parameter['orderby']='createtime desc';
		$linelog=$this->get_list('worker_linelog',$parameter);
		$item['lineend']=$linelog[0]['createtime'];
		$item['dayliushinum']=price(($item['lineend']-$item['linestart'])/3600);
	}else{
		$item['dayliushinum']=0;
	}
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$link_time[0]=0;
	$link_time[1]=$daytime-3*24*3600;
	$link_time[2]=$daytime-7*24*3600;
	$link_time[3]=$daytime-15*24*3600;
	$link_time[4]=$daytime-30*24*3600;
	$link_time[5]=$daytime-90*24*3600;
	$link_time[6]=$daytime-180*24*3600;
	$link_times=$this->link_times;
	$return['optype']='log';
	if($item['genjintime']){
		$item['genjintime']=date('Y-m-d',$item['genjintime']);
	}
	if($item['nextgenjintime']){
		$item['nextgenjintime']=date('Y-m-d',$item['nextgenjintime']);
	}
	$item['tags']=json_decode($item['tags'],true);
	$return['item']=$item;
	$return['post']=array('id'=>$item['id'],'buystatus'=>$item['buystatus']?:1);
	set_html('title','客户详情');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='log'){
	$userid=intval($_RQ['id']);
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'userid'=>$userid);
	if(isset($_RQ['istop'])){
		$params['istop']=intval($_RQ['istop']);
		$return['params']['istop']=$_RQ['istop'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_worker_linelog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $k=>&$v){
		$v['worker']=pdo_get('jy_scrm_worker',array('id'=>$v['workerid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'));	
	}
	$return['user_buystatus']=$this->user_buystatus;
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($_RQ['postdata']){
		$workerid=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid']),'id');
		$postdata=get_postdata();
		$nexttime=intval(strtotime($postdata['nexttime']))?:TIMESTAMP+2*24*3600;
		$data=array(
			'content'=>trim($postdata['content']),
			'workerid'=>$workerid,
			'userid'=>$postdata['id'],
			'isout'=>1,
			'istop'=>intval($postdata['istop']),
			'nexttime'=>$nexttime,
			'createtime'=>TIMESTAMP,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_scrm_worker_linelog',$data);
		pdo_update('jy_scrm_member',array('genjintime'=>TIMESTAMP,'nextgenjintime'=>$nexttime,'star'=>$postdata['star'],'buystatus'=>$postdata['buystatus']?:1),array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('跟进成功！','','jy_merchscrm/member.item/main/id='.$postdata['id']);
	}
	$return['item']=$item;
	$return['post']=array('id'=>$id,'star'=>$item['star'],'buystatus'=>$item['buystatus']?:'1');
	exi($return);
}
if($_RQ['op']=='remark'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['buydays']=$item['buydays']?json_decode($item['buydays'],true):array('one'=>'no');
	if($item['birthtime']){
		$item['birth']=array(
			'year'=>date('Y',$item['birthtime']),
			'month'=>date('m',$item['birthtime']),
			'day'=>date('d',$item['birthtime']),
		);
	}else{
		$item['birth']=array('year'=>'1988');
	}
	if($item['jiehun']){
		$item['jiehun']=array(
			'year'=>date('Y',$item['jiehun']),
			'month'=>date('m',$item['jiehun']),
			'day'=>date('d',$item['jiehun']),
		);
	}else{
		$item['jiehun']=array('year'=>'1988');
	}
	if($item['buyday']){
		$item['buyday']=array(
			'year'=>date('Y',$item['buyday']),
			'month'=>date('m',$item['buyday']),
			'day'=>date('d',$item['buyday']),
			'goods'=>$item['buydaygoods'],
		);
	}else{
		$item['buyday']=array('year'=>date('Y',time()));
	}
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$member=pdo_get('jy_scrm_member',array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		$birthtime=$postdata['birth']['year'].'-'.$postdata['birth']['month'].'-'.$postdata['birth']['day'];
		$jiehun=$postdata['jiehun']['year'].'-'.$postdata['jiehun']['month'].'-'.$postdata['jiehun']['day'];
		$buyday=$postdata['buyday']['year'].'-'.$postdata['buyday']['month'].'-'.$postdata['buyday']['day'];
		foreach($postdata['remark_mobiles'] as $mv){
			if($mv && (strlen($mv)!=11 || $mv[0]!='1' || !is_numeric($mv))){
				exi('手机号码格式错误，请输入11位，1开头的手机号！！','error');
			}
		}
		if($postdata['buydays']){
			$lastbuy=0;
			$buydays=array();
			foreach($postdata['buydays'] as $bv){
				if($bv=='no'){
					continue;
				}
				$btime=$bv['year'].'-'.$bv['month'].'-'.$bv['day'];
				$btime=strtotime($btime);
				$btime=$btime?:'one';
				$buydays[$btime]=$bv;
				if($lastbuy<$btime){
					$lastbuy=$btime;
				}
			}
			if(count($buydays)==0){
				$buydays=array('one'=>'no');
			}
		}elseif($buyday){
			$lastbuy=strtotime($buyday);
		}
		$shoubuyday=strtotime($buyday);
		$lastbuy=$shoubuyday>$lastbuy?$shoubuyday:$lastbuy;
		$update=array(
			'remark'=>$postdata['remark'],
			'remark_company'=>$postdata['remark_company'],
			'remark_mobiles'=>ijson_encode($postdata['remark_mobiles']),
			'remark'=>$postdata['remark'],
			'birthtime'=>strtotime($birthtime),
			'jiehun'=>strtotime($jiehun),
			'buyday'=>strtotime($buyday),
			'buydaygoods'=>$postdata['buyday']['goods'],
			'buydays'=>ijson_encode($buydays),
			'lastbuy'=>$lastbuy,
		);
		if($update['birthtime']){
			$update['birth']=date('Y-m-d',$update['birthtime']);
		}
		if($update['jiehun']){
			$update['jiehunday']=date('Y-m-d',$update['jiehun']);
		}
		if($update['buyday']){
			$update['buyday2']=date('Y-m-d',$update['buyday']);
		}
		pdo_update('jy_scrm_member',$update,array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		$update['userid']=$member['follow_user_userid']?:$member['follow_user_oper_userid'];
		$update['external_userid']=$member['external_userid'];
		$update['remark_mobiles']=json_decode($update['remark_mobiles'],true);
		model('jy_scrm/member')->up_member_remark($update);
		if(!$update['userid']){
			exi('未绑定员工，请先后台同步客户！','error');
		}
		exi('备注成功！','','jy_merchscrm/member.item/main/id='.$postdata['id']);
	}
	if(strpos($item['remark_mobiles'],'[') !== false){
		$item['remark_mobiles']=json_decode($item['remark_mobiles'],true);
	}else{
		$item['remark_mobiles']=array($item['remark_mobiles']);
	}
	$return['item']=$item;
	$return['post']=array(
		'id'=>$id,
		'remark'=>$item['remark'],
		'remark_company'=>$item['remark_company'],
		'remark_mobiles'=>$item['remark_mobiles'],
		'birth'=>$item['birth'],
		'jiehun'=>$item['jiehun'],
		'buyday'=>$item['buyday'],
		'buydays'=>$item['buydays'],
	);
	$return['years']=array();
	for($i=1966;$i<2028;$i++){
		$return['years'][]=$i;
	}
	$return['months']=array('01','02','03','04','05','06','07','08','09','10','11','12');
	$return['days']=array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
	exi($return);
}
if($_RQ['op']=='getorder'){
	$mid=$this->session('user_mid');
	$mid=$mid?:-1;
	//显示统计数据
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$order['all']=pdo_count('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1));
	$order['day']=pdo_count('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$daytime));
	$order['moneyall']=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1),'price');
	$order['moneyday']=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$daytime),'price');
	$return['order']=$order;
	exi($return);
}
if($_RQ['op']=='getorderlist'){
	$mid=$this->session('user_mid');
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'userdeleted'=>0);
	$params['mid']=$mid?:-1;
	if(isset($_RQ['status']) && $_RQ['status'] !=100){
		$params['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=intval($_RQ['ordersn']);
	}
	if(!empty($_RQ['userdeleted'])){
		$params['userdeleted']=1;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','price','addprice','ispayadd','createtime','ispay','mid','paytime','status','buytype','sendtype','ordersn','addressid','dispatchprice');
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	$feemoneyset=plugin('jy_weishop')->plugin_setting('feemoney');
	$feemoneyset['feerade']=$feemoneyset['feerade']?:5;
	foreach($list as &$item){
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
		$merchs =array();
		$item['feemoney']=0;
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			if($_SESSION['is_tomedia']){
			$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
			}
			$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
			if($feemoneyset['isopen']==1){
				$gv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$item['price']:$feemoneyset['feerade']*$item['price']/100;
				$gv['feemoney']=$gv['total']*$gv['feemoney'];
				$item['feemoney'] +=$gv['feemoney'];
				$goodsinfo['feemoney']=price($gv['feemoney']);
			}
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
			}else{
				$merchs[0]['goods'][] = $goodsinfo;
			}
		}
		$item['feemoney']=price($item['feemoney']);
		//获取地址
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && $item['address']['name']){	
			$address=$item['address'];
		}else{
			$address=$item['addressid'];
		}
		$item['address']=model('core/member')->get_address_str($address);
		$item['pingjiatime'] = pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid'=>0),'createtime');
		if($item['pingjiatime']){
			$comment_changedays=$shopset['comment_changedays']?:0;
			$endpingjiatime = $item['pingjiatime'] + $comment_changedays*24*3600;
			if($endpingjiatime>TIMESTAMP){
				$item['pingjia']=1;//1修改评价；2追加评价
			}else{
				$zuipingjia = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid >'=>0),'id');
				if($zuipingjia<1){
					$item['pingjia']=2;
				}else{
					$item['pingjia']=3;
				}
			}
		}
		$item['merchs']=$merchs;
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		$item['statustitle']=plugin('jy_weishop')->order_status[$item['status']];
		$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		$item['can_refund']=model('jy_weishop/order/refund')->check_order_rerefund($item['id']);
	}
	$return['list']=$list;
	exi($return);
}
?>